<template>
    <view>
        <view class="addrcontent" v-if="ctype == 0 && addr.name">
            <view>
                <view @tap="goaddr">
                    <image src="/static/dizhi1.png" mode="aspectFit"></image>
                    <view class="addr">
                        <view>
                            <text>收件人:{{ addr.name }}</text>
                            <text>{{ addr.phone }}</text>
                        </view>
                        <text>{{ addr.sheng }}{{ addr.shi }}{{ addr.qu }}{{ addr.address }}</text>
                    </view>
                </view>
                <image src="/static/arrow-b.png" mode="aspectFit"></image>
            </view>
            <image src="/static/addrbg.png" mode="aspectFit"></image>
        </view>
        <view class="addrcontent" v-if="ctype == 0 && !addr.name">
            <view>
                <view @tap="goaddr">
                    <image src="/static/dizhi1.png" mode="aspectFit"></image>
                    <view class="addr">
                        <view style="margin-top: 5px">
                            <text style="color: rgb(145, 143, 143)">请设置收货地址</text>
                        </view>
                    </view>
                </view>
                <image src="/static/arrow-b.png" mode="aspectFit"></image>
            </view>
            <image src="/static/addrbg.png" mode="aspectFit"></image>
        </view>
        <!-- <view class="noaddr" wx:if="{{ctype==0 && !addr}}" bindtap="getwxaddr">
    <text>获取微信收货地址</text>
  </view> -->
        <view class="goods">
            <!-- <view class="shoptitle">
    <image src="/static/dianpu1.png" mode="aspectFit"></image>
    <text>优汇多</text>
    <image src="/static/right.png" mode="aspectFit"></image>
  </view> -->
            <view class="goodslist" v-for="(item, index) in goodslist" :key="index">
                <image :src="item.thumb" mode="aspectFill"></image>

                <view class="msgs">
                    <text class="ellipsis-2">{{ item.title }}</text>
                    <!-- <text v-if="info.guige[index]">{{ info.guige[index] }}</text> -->
                    <view>
                        <text>￥{{ item.price }}</text>
                        <text>X{{ info.num }}</text>
                    </view>
                </view>
            </view>
        </view>
        <view class="lists">
            <view class="slist">
                <text>快递费</text>
                <text>免运费</text>
            </view>
            <view class="liuyan">
                <text>买家留言</text>
                <input type="text" placeholder="请输入内容" v-model="memo" />
            </view>
            <view class="price">
                <text>
                    共{{ info.num }}件商品 合计
                    <text>￥{{ info.money }}</text>
                </text>
            </view>
        </view>

        <view class="bottom">
            <text>
                合计:
                <text>￥{{ info.money }}</text>
            </text>
            <!-- <text @tap="gopay">提交订单</text> -->
            <text @tap="submit">提交订单</text>
        </view>
        <view class="tan" v-if="ispay">
            <view class="shadow" @tap="close"></view>
            <view class="content" :style="'padding-bottom:' + (isipx ? 10 : 0) + 'px;'">
                <view class="title">
                    <text></text>
                    <text>请选择支付方式</text>
                    <image style="opacity: 0" src="/static/close.png" mode="aspectFit"></image>
                </view>
                <radio-group @change="paychange">
                    <label>
                        <view class="tlist">
                            <view>
                                <image src="/static/wchat.png" mode="aspectFit"></image>
                                <text>微信</text>
                            </view>
                            <radio value="wxpay" v-model="paytype" :checked="paytype == 'wxpay'"></radio>
                        </view>
                    </label>
<!-- 
                    <label>
                        <view class="tlist">
                            <view>
                                <image src="/static/money.png" mode="aspectFit"></image>
                                <text>线下转帐</text>
                            </view>
                            <radio value="unline" v-model="paytype" :checked="paytype == 'unline'"></radio>
                        </view>
                    </label> -->
                </radio-group>

                <view class="btns" @tap="submit">确认</view>
            </view>
        </view>
    </view>
</template>

<script>
// import header from '../../components/header/headerBar.vue';

// pages/index/submit.js
// var app = getApp();
export default {
    data() {
        return {
            ispay: 0,
            type: 1,
            flag: true,
            goodstotal: 0,
            paytype: 'unline',
            ctype: 0,
			memo:'',
            addr: {
                name: '',
                phone: '',
                sheng: '',
                shi: '',
                qu: '',
                address: ''
            },

            isipx: '',
            orderid: '',
            cartid: '',
            is_fudou: '',
            content: '',
            msgcode: '',

            info: {
                goodslist: [],
                guige: '',
                yunfei: '',
                money: ''
            },
			goodslist:[]
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.orderid= options.orderid,
        this.cartid= options.cartid
		this.$http('center/addresslist',{p:1},function(res){
			if (res.code === 0) {
			        var res = res.data;
			
			        if (res.list) {
			            that.addr = res.list[0]
			        }
			    }
			
		})
		// that.getorder();
        
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        var pages = getCurrentPages();
        var currpage = pages[pages.length - 1];

        if (currpage.addr) {
            that.addr= currpage.addr
        }
		that.getorder();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    methods: {
        changetab(e) {
            var type = e.currentTarget.dataset.type;
            this.ctype= type
            
            this.getorder();
        },

        getwxaddr() {
            var _this = this;

            uni.chooseAddress({
                success(res) {
                    _this.addr = {
                        name: res.userName,
                        phone: res.telNumber,
                        sheng: res.provinceName,
                        shi: res.cityName,
                        qu: res.countyName,
                        address: res.detailInfo,
                        is_moren: 1
                    };
                    app.globalData.authPost2('center/addaddr', _this.addr, function (res) {
                        uni.hideLoading();
                        _this.addr.id = res.id;

                        _this.setData({
                            addr: _this.addr
                        });
                    });
                },

                fail(res) {
                    console.log(res);
                    uni.openSetting({
                        success(res) {
                            console.log(res.authSetting); // res.authSetting = {
                            //   "scope.userInfo": true,
                            //   "scope.userLocation": true
                            // }
                        }
                    });
                }
            });
        },

        goaddr() {
            uni.navigateTo({
                url: '/pages/address/list?isselect=1'
            });
        },

        close() {
            this.ispay= 0
            
        },

        gopay() {
            var _this = this;

            this.ispay= 1
            
        },

        paychange(e) {
            this.setData({
                paytype: e.detail.value
            });
        },

        changestatus(e) {
            this.setData({
                is_fudou: e.detail.value[0] ? 1 : 0
            });
        },

        onInput(e) {
            this.setData({
                content: e.detail.value
            });
        },

        submit() {
            var _this = this;
			this.ispay = 0;
            if (!this.addr.name && this.ctype == 0) {
                uni.showToast({
                    title: '请选择收货地址',
                    icon: 'none'
                });
                return false;
            }

            if (this.flag) {
                _this.flag= false
				var ua=window.navigator.userAgent.toLowerCase()
				if(_this.info.model=='vip'){
					
						if(ua.match(/MicroMessenger/i)=='micromessenger'){
							var openid = window.localStorage.getItem('wxopenid');
							var paytype = 'jsapi';
						}else{
							var paytype = 'mweb';
							var openid = '';
						}
					    _this.$http(
					        'center/enterorder',
					        {
					            address: _this.addr ? _this.addr : [],
					            orderid: _this.orderid,
					            memo: _this.memo,
					            paytype: paytype,
					            isziti: 0,
								openid:openid
					        },
					        function (res) {
					            if (res.code === 0) {
									if(paytype=='mweb'){
										window.location.href = res.data.mweb_url;	
									}else{
										function onBridgeReady(){
										   WeixinJSBridge.invoke(
										      'getBrandWCPayRequest', {
										         "appId":res.data.paydata.appid,     //公众号ID，由商户传入     
										         "timeStamp":res.data.paydata.timestamp,         //时间戳，自1970年以来的秒数     
										         "nonceStr":res.data.paydata.nonceStr, //随机串     
										         "package":res.data.paydata.package,     
										         "signType":"MD5",         //微信签名方式：     
										         "paySign":res.data.paydata.sign //微信签名 
										      },
										      function(res){
										      if(res.err_msg == "get_brand_wcpay_request:ok" ){
										      // 使用以上方式判断前端返回,微信团队郑重提示：
										            //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对
													uni.showModal({
														title:'支付提示',
														content:'支付成功',
														showCancel:false,
														success() {
															uni.reLaunch({
																url:'/pages/order/list?status=1'
															})
														}
													})
										      } 
										   }); 
										}
										if (typeof WeixinJSBridge == "undefined"){
										   if( document.addEventListener ){
										       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
										   }else if (document.attachEvent){
										       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
										       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
										   }
										}else{
											uni.showLoading({
												title:'微信通信...'
											})
											setTimeout(function(){
												uni.hideLoading();
											},1500)
										   onBridgeReady();
										}
									}
					            } else {
					                _this.flag= true;
					
					                uni.showToast({
					                    title: res.msg,
					                    icon: 'none'
					                });
					            }
					        }
					    );
					
				}
				if(_this.info.model!='vip'){
					if(ua.match(/MicroMessenger/i)=='micromessenger'){
						var openid = window.localStorage.getItem('wxopenid');
						var paytype = 'jsapi';
					}else{
						var paytype = 'mweb';
						var openid = '';
					}
					uni.showModal({
						title:'购买提示',
						content:'购买该商品奖扣除您'+_this.info.money+'的积分，确认购买吗？',
						confirmText:'确认购买',
						success(e) {
							if(e.confirm){
								_this.$http(
								    'center/enterorder',
								    {
								        address: _this.addr ? _this.addr : [],
								        orderid: _this.orderid,
								        memo: _this.memo,
								        openid:openid
								    },
								    function (res) {
								        if (res.code === 0) {
											uni.showModal({
												title:'支付提示',
												content:'支付成功',
												showCancel:false,
												success() {
													uni.reLaunch({
														url:'/pages/order/list?status=1'
													})
												}
											})
								        } else {
								            _this.flag= true;
													
								            uni.showToast({
								                title: res.msg,
								                icon: 'none'
								            });
								        }
								    }
								);
								
							}
						}
					})
										
				}
			}
        },

        getorder() {
            var _this = this;

            _this.$http(
                'center/getorder',
                {
                    orderid: _this.orderid,
                    aid: _this.addr.id,
					address:_this.addr,
                    paytype: _this.ctype
                },
                function (res) {
                    if (res.code === 0) {
                        _this.info=res.data
						_this.goodslist = [res.data.goods]
                    }
                }
            );
        }
    }
};
</script>
<style>
@import './submitorder.scss';
</style>
